package com.example.wangzh.developkit.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.constant.LogTag;
import com.example.wangzh.developkit.utils.LogUtils;

import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.List;

/**
 * qz
 * wzh
 * 2017.5.15
 */
public class SegmentView extends LinearLayout {
	private onSegmentViewClickListener listener;
	private OnSelectChangeListener onSelectChangeListener;
	private Context context;
	private int selectPosition = 0;
	List<TextView> textViews = new ArrayList<>( );
	String[] strs = new String[]{"金属", "非金属"};
	int textColorResId;

	public SegmentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public SegmentView(Context context) {
		super(context, null);
	}

	public int getSelectPositon() {
		return selectPosition;
	}

	private void init(Context context, AttributeSet attrs) {
		this.context = context;
		loadAttrs(attrs);
		this.setLayoutParams(new
				LinearLayout.LayoutParams(dp2Px(getContext( ), 60),
				LinearLayout.LayoutParams.WRAP_CONTENT));
		createTextView( );
		addTextView( );
	}

	private void addTextView() {
		this.removeAllViews( );
		for (int i = 0; i < textViews.size( ); i++) {
			final int index = i;
			textViews.get(i).setOnClickListener(new OnClickListener( ) {
				@Override
				public void onClick(View v) {
					if (v.isSelected( )) {
						return;
					}
					refreshSelected(index);
					if (listener != null) {
						listener.onSegmentViewClick(v, index);
					}
					if (onSelectChangeListener != null) {
						onSelectChangeListener.onSelectChange(index);
					}
				}
			});
			addView(textViews.get(i));
			if (i < textViews.size( ) - 1) {
				addDivider( );
			}
		}
	}

	private void addDivider() {
		View divider = new View(getContext( ));
		divider.setLayoutParams(new LayoutParams(dp2Px(getContext( ), 1), ViewGroup.LayoutParams.MATCH_PARENT));
		divider.setBackgroundResource(R.color.white);
		addView(divider);
	}

	private void refreshSelected(int index) {
		for (int i = 0; i < textViews.size( ); i++) {
			textViews.get(i).setSelected(i == index);
		}
	}

	public void setSelect(int position) {
		if (position < 0) {
			position = 0;
		}
		if (position > textViews.size( ) - 1) {
			position = textViews.size( ) - 1;
		}
		if (selectPosition != position) {
			selectPosition = position;
			refreshSelected(selectPosition);
			if (onSelectChangeListener != null) {
				onSelectChangeListener.onSelectChange(selectPosition);
			}
		}

	}

	private void createTextView() {
		textViews.clear( );
		for (int i = 0; i < strs.length; i++) {
			TextView txt = new TextView(getContext( ));
			txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, dp2Px(getContext( ), 24), 1));
			txt.setPadding(30, 6, 30, 6);
			txt.setGravity(Gravity.CENTER);
			txt.setText(strs[i]);
			txt.setTextSize(12);
			setTextColorRes(txt);
			if (i == 0) {
				txt.setBackgroundResource(R.drawable.selector_seg_left);
				txt.setSelected(true);
			} else if (i == strs.length - 1) {
				txt.setBackgroundResource(R.drawable.selector_seg_right);
			} else {
				txt.setBackgroundResource(R.drawable.selector_seg_center);
			}
			textViews.add(txt);
		}

	}

	/**
	 * @param txt 如果设置了字体颜色selector则用设置的，否则用默认的
	 */
	@SuppressLint("ResourceType")
	private void setTextColorRes(TextView txt) {
		XmlPullParser xrp;
		if (textColorResId == 0) {
			xrp = getResources( ).getXml(R.drawable.seg_text_color_selector);
		} else {
			xrp = getResources( ).getXml(textColorResId);
		}
		try {
			ColorStateList csl = ColorStateList.createFromXml(getResources( ), xrp);
			txt.setTextColor(csl);
		} catch (Exception e) {
		}
	}

	private void loadAttrs(AttributeSet attrs) {
		if (attrs == null) {
			return;
		}
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SegmentView);
		int strsResId = a.getResourceId(R.styleable.SegmentView_Seg_strs, 0);
		if (strsResId == 0) {
			LogUtils.i(LogTag.ERROR, getClass( ).getSimpleName( ) + ":加载参数出错");
			return;
		}
		strs = getResources( ).getStringArray(strsResId);

		textColorResId = a.getResourceId(R.styleable.SegmentView_Seg_text_selector, 0);
	}

	private static int dp2Px(Context context, float dp) {
		final float scale = context.getResources( ).getDisplayMetrics( ).density;
		return (int) (dp * scale + 0.5f);
	}

	@Deprecated
	public void setOnSegmentViewClickListener(onSegmentViewClickListener listener) {
		this.listener = listener;
	}

	public void setOnSelectChangeListener(OnSelectChangeListener onSelectChangeListener) {
		this.onSelectChangeListener = onSelectChangeListener;
	}

	public interface onSegmentViewClickListener {
		void onSegmentViewClick(View v, int position);
	}

	public interface OnSelectChangeListener {
		void onSelectChange(int position);
	}

}
